using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Kxzz.Controls
{
	/**
	 *  A RolloverPanel is a Panel that changes an interior font when the mouse enters / leaves its area
	 */
	public class rollOverPanel : Panel
	{
		#region Properties
		
		private Label textLabel;
		private PictureBox iconPictureBox;

		Font _rollOverFont;
		Font _rollOutFont;
		Cursor _rollOverCursor;
		Color _rollOverForeColor;
		Color _rollOverBackColor;
		Color _rollOutForeColor;
		Color _rollOutBackColor;

		#endregion

		public rollOverPanel()
		{
			// Avoid flicker on redraw
			this.DoubleBuffered = true;
			this.SetStyle(ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);

			InitializeComponent();
			_rollOverFont = null;
			_rollOutFont = textLabel.Font;
			_rollOverCursor = Cursors.Hand;
			_rollOverForeColor = textLabel.ForeColor;
			_rollOverBackColor = textLabel.BackColor;
			this.MouseEnter += new System.EventHandler(this.mouseRollOver);
			this.MouseLeave += new System.EventHandler(this.mouseRollOut);
			textLabel.MouseEnter += new System.EventHandler(this.mouseRollOver);
			textLabel.MouseLeave += new System.EventHandler(this.mouseRollOut);
			iconPictureBox.MouseEnter += new System.EventHandler(this.mouseRollOver);
			iconPictureBox.MouseLeave += new System.EventHandler(this.mouseRollOut);
		}

		private void InitializeComponent()
		{
			this.iconPictureBox = new System.Windows.Forms.PictureBox();
			this.textLabel = new System.Windows.Forms.Label();
			((System.ComponentModel.ISupportInitialize)(this.iconPictureBox)).BeginInit();
			this.SuspendLayout();
// 
// iconPictureBox
// 
			this.iconPictureBox.BackColor = System.Drawing.Color.Transparent;
			this.iconPictureBox.Location = new System.Drawing.Point(9, 14);
			this.iconPictureBox.Margin = new System.Windows.Forms.Padding(0);
			this.iconPictureBox.Name = "iconPictureBox";
			this.iconPictureBox.Size = new System.Drawing.Size(16, 16);
			this.iconPictureBox.TabIndex = 0;
			this.iconPictureBox.TabStop = false;
// 
// textLabel
// 
			this.textLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
						| System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.textLabel.AutoSize = true;
			this.textLabel.BackColor = System.Drawing.Color.Transparent;
			this.textLabel.Location = new System.Drawing.Point(230, 22);
			this.textLabel.Margin = new System.Windows.Forms.Padding(0);
			this.textLabel.Name = "textLabel";
			this.textLabel.Size = new System.Drawing.Size(87, 14);
			this.textLabel.TabIndex = 0;
			this.textLabel.Text = "(Insert text here)";
			this.textLabel.Resize += new System.EventHandler(this.textLabel_Resize);
// 
// rollOverPanel
// 
			this.BackColor = System.Drawing.Color.Transparent;
			this.Controls.Add(this.textLabel);
			this.Controls.Add(this.iconPictureBox);
			this.Size = new System.Drawing.Size(200, 20);
			this.Resize += new System.EventHandler(this.rollOverPanel_Resize);
			((System.ComponentModel.ISupportInitialize)(this.iconPictureBox)).EndInit();
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		#region Property Definitions

		public Font RollOverFont
		{
			get
			{
				return _rollOverFont;
			}
			set
			{
				_rollOverFont = value;
				this.Invalidate();
			}
		}

		public Cursor RollOverCursor
		{
			get
			{
				return _rollOverCursor;
			}
			set
			{
				_rollOverCursor = value;
				this.Invalidate();
			}
		}

		public Color RollOverForeColor
		{
			get
			{
				return _rollOverForeColor;
			}
			set
			{
				_rollOverForeColor = value;
				this.Invalidate();
			}
		}
		public Color RollOverBackColor
		{
			get
			{
				return _rollOverBackColor;
			}
			set
			{
				_rollOverBackColor = value;
				this.Invalidate();
			}
		}

		public Image IconImage
		{
			get
			{
				return this.iconPictureBox.Image;
			}
			set
			{
				this.iconPictureBox.Image = value;
				this.Invalidate();
			}
		}

		public String LabelText
		{
			get
			{
				return this.textLabel.Text;
			}
			set
			{
				this.textLabel.Text = value;
				this.Invalidate();
			}
		}

		public Point ImageOffset
		{
			get
			{
				return this.iconPictureBox.Location;
			}
			set
			{
				this.iconPictureBox.Location = value;
				this.Invalidate();
			}
		}

		public Size ImageSize
		{
			get
			{
				return this.iconPictureBox.Size;
			}
			set
			{
				this.iconPictureBox.Size = value;
				this.Invalidate();
			}
		}

		public Font LabelFont
		{
			get
			{
				return this.textLabel.Font;
			}
			set
			{
				this.textLabel.Font = value;
				this.Invalidate();
			}
		}

		public Point LabelOffset
		{
			get
			{
				return this.textLabel.Location;
			}
			set
			{
				this.textLabel.Location = value;
				this.Invalidate();
			}
		}

		public Color LabelForeColor
		{
			get
			{
				return this.textLabel.ForeColor;
			}
			set
			{
				this.textLabel.ForeColor = value;
				this.Invalidate();
			}
		}

		public Padding LabelMargin 
		{
			get
			{
				return this.textLabel.Margin;
			}
			set
			{
				this.textLabel.Margin = value;
				this.Invalidate();
			}
		}

		public Padding PictureMargin
		{
			get
			{
				return this.iconPictureBox.Margin;
			}
			set
			{
				this.iconPictureBox.Margin = value;
				this.Invalidate();
			}
		}

		#endregion

		/**
		 *  Event handler that manages font and cursor changes when the mouse hovers over the RolloverPanel
		 */
		private void mouseRollOver(object sender, EventArgs e)
		{
			this.Cursor = _rollOverCursor;

			if (RollOverFont != null)
			{
				_rollOutFont = this.textLabel.Font;
				this.textLabel.Font = _rollOverFont;
			}

			_rollOutForeColor = this.textLabel.ForeColor;
			this.textLabel.ForeColor = _rollOverForeColor;

			_rollOutBackColor = this.textLabel.BackColor;
			this.textLabel.BackColor = _rollOverBackColor;
		}

		/**
		 *  Event handler that manages font and cursor changes when the mouse moves off the RolloverPanel
		 */
		private void mouseRollOut(object sender, EventArgs e)
		{
			this.Cursor = Cursors.Default;

			if (_rollOutFont != null)
			{
				this.textLabel.Font = _rollOutFont;
			}

			this.textLabel.ForeColor = _rollOutForeColor;
			this.textLabel.BackColor = _rollOutBackColor;
		}

		/**
		 *  Handles resizing the label withing the panel when the panel is resized
		 */
		private void rollOverPanel_Resize(object sender, EventArgs e)
		{
			this.textLabel.MaximumSize = new Size(this.Width - (this.textLabel.Location.X), 0);
		}

		/**
		 *  Adjusts the height of the RolloverPanel when the label height shrinks or grows
		 */
		private void textLabel_Resize(object sender, EventArgs e)
		{
			this.Height = (this.textLabel.Size.Height + this.textLabel.Margin.Vertical);
		}
	}
}
